package com.ruoyi.other.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.other.domain.CityConfig;
import com.ruoyi.other.domain.CityFareConfig;
import com.ruoyi.other.domain.Region;
import com.ruoyi.other.mapper.CityConfigMapper;
import com.ruoyi.other.mapper.CityFareConfigMapper;
import com.ruoyi.other.mapper.RegionMapper;
import com.ruoyi.other.service.ICityConfigService;
import com.ruoyi.other.vo.AddCityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 城市管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-16
 */
@Service
public class CityConfigServiceImpl implements ICityConfigService {
    @Autowired
    private CityConfigMapper cityConfigMapper;
    @Resource
    private CityFareConfigMapper cityFareConfigMapper;
    @Resource
    private RegionMapper regionMapper;

    /**
     * 查询城市管理
     *
     * @param id 城市管理主键
     * @return 城市管理
     */
    @Override
    public CityConfig selectCityConfigById(Long id) {
        return cityConfigMapper.selectCityConfigById(id);
    }

    /**
     * 查询城市管理列表
     *
     * @param cityConfig 城市管理
     * @return 城市管理
     */
    @Override
    public List<CityConfig> selectCityConfigList(CityConfig cityConfig) {
        List<CityConfig> cityConfigs = cityConfigMapper.selectAllCity();
        Map<Long, CityConfig> cityMap = new HashMap<>();
        List<CityConfig> rootCities = new ArrayList<>();
        if (CheckEmptyUtil.isEmpty(cityConfigs)) {
            return Collections.emptyList();
        }
        // 第一步：将所有城市转换为AddCityFareCityVo对象，并放入Map中
        for (CityConfig cityConfig1 : cityConfigs) {
            CityConfig cityVo = new CityConfig();
            cityVo.setId(cityConfig1.getId());
            cityVo.setPartnerId(cityConfig1.getPartnerId());
            cityVo.setCityName(cityConfig1.getCityName());
            cityVo.setCreateTime(cityConfig1.getCreateTime());
            cityVo.setChildren(new ArrayList<>());
            cityMap.put(cityConfig1.getId(), cityVo);
        }

        // 第二步：构建城市树结构
        for (CityConfig cityConfig2 : cityConfigs) {
            CityConfig cityVo = cityMap.get(cityConfig2.getId());
            if (cityConfig2.getPartnerId() == 0) {
                // 如果是顶级城市，则添加到根城市列表中
                rootCities.add(cityVo);
            } else {
                // 否则，添加到对应父城市的children列表中
                CityConfig parentCityVo = cityMap.get(cityConfig2.getPartnerId());
                if (parentCityVo != null) {
                    parentCityVo.getChildren().add(cityVo);
                }
            }
        }

        return rootCities;
        //return cityConfigMapper.selectCityConfigList(cityConfig);
    }

    /**
     * 新增城市管理
     *
     * @param cityConfig 城市管理
     * @return 结果
     */
    @Override
    public int insertCityConfig(CityConfig cityConfig) {
        cityConfig.setCreateTime(DateUtils.getNowDate());
        return cityConfigMapper.insertCityConfig(cityConfig);
    }

    /**
     * 修改城市管理
     *
     * @param cityConfig 城市管理
     * @return 结果
     */
    @Override
    public int updateCityConfig(CityConfig cityConfig) {
        cityConfig.setUpdateTime(DateUtils.getNowDate());
        return cityConfigMapper.updateCityConfig(cityConfig);
    }

    /**
     * 批量删除城市管理
     *
     * @param ids 需要删除的城市管理主键
     * @return 结果
     */
    @Override
    public int deleteCityConfigByIds(Long[] ids) {
        if (!CheckEmptyUtil.isEmpty(ids)) {
            for (Long id : ids) {
                CityConfig cityConfig = cityConfigMapper.selectCityConfigById(id);
                if (cityConfig.getPartnerId() == 0) {
                    Integer childrenCount = cityConfigMapper.seleChildrenCount(cityConfig.getPartnerId());
                    if (childrenCount > 0) {
                        throw new RuntimeException("请先删除下级城市数据");
                    }
                }
                CityFareConfig cityFareConfig = cityFareConfigMapper.selectOne(cityConfig.getPartnerId(), cityConfig.getId());
                if (!CheckEmptyUtil.isEmpty(cityFareConfig)) {
                    throw new RuntimeException("请先删除城市路费设置数据");
                }
            }
        }
        return cityConfigMapper.deleteCityConfigByIds(ids);
    }

    /**
     * 删除城市管理信息
     *
     * @param id 城市管理主键
     * @return 结果
     */
    @Override
    public int deleteCityConfigById(Long id) {
        return cityConfigMapper.deleteCityConfigById(id);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Integer addCity(AddCityVo addCityVo) {
        Integer result = 0;
        Region region = regionMapper.selectRegionById(addCityVo.getId());
        if (region.getPid() == 0) {
            throw new RuntimeException("请选择二级城市");
        }else {
            Region partner = regionMapper.selectRegionById(region.getPid());
            CityConfig partnerCity = cityConfigMapper.selectPartner(partner.getName());
            CityConfig cityConfig = cityConfigMapper.selectCity(partner.getName(), region.getName());
            if (!CheckEmptyUtil.isEmpty(cityConfig)){
                throw new RuntimeException("该城市已添加");
            }
            if (CheckEmptyUtil.isEmpty(partnerCity)){
                CityConfig insertPartner = new CityConfig();
                insertPartner.setCityName(partner.getName());
                insertPartner.setPartnerId(0L);
                cityConfigMapper.insertCityConfig(insertPartner);

                CityConfig insertCityConfig = new CityConfig();
                insertCityConfig.setCityName(region.getName());
                insertCityConfig.setPartnerId(insertPartner.getId());
                result = cityConfigMapper.insertCityConfig(insertCityConfig);
            }else {
                CityConfig insertCityConfig = new CityConfig();
                insertCityConfig.setCityName(region.getName());
                insertCityConfig.setPartnerId(partnerCity.getId());
                result = cityConfigMapper.insertCityConfig(insertCityConfig);
            }

        }

        return result;
    }
}
